import Vue from 'vue'

import Router from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Order from '../views/Order.vue'
import Me from '../views/Me.vue'
import Goods from '../views/Goods.vue'
import List from '../views/List.vue'
import File from '../views/File.vue'
Vue.use(Router)

const router= new Router({
  routes: [
     {path:'/logout',
     redirect:'/login',
     },

    {
      path:'/login',
      name:'Login',
      component:Login
    },
    {
      path:'/home',
      name:'Home',
      component:Home
    },
    {
      path:'/order',
      name:'Order',
      component:Order
    },
    {
      path:'/me',
      name:'Me',
      component:Me
    },
    {
      path:'/file',
      name:'File',
      component:File
    },
    {
      path:'/goods',
      name:'Goods',
      component:Goods
    },
    {
      path:'/list',
      name:'List',
      component:List
    }
  ]
});

//导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  //debugger
  if (to.path === '/login') {
    next();
  } else {
    let token = sessionStorage.getItem('Authorization');
    if (!token) {
      next('/login');
    } else {
      next();
    }
  }
});

export default router;
